﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChuanYe.WorkLogManagement.Api.Models;
using ChuanYe.WorkLogManagement.BLL;
using ChuanYe.WorkLogManagement.EF.SqlServer;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ChuanYe.WorkLogManagement.Api.Controllers
{
    /// <summary>
    /// 部门接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DepartmentController : ControllerBase
    {
        /// <summary>
        /// 增加 部门
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<int> Add(DepartmentRequest obj)
        {
            ResultDto<int> result = new ResultDto<int>();
            try
            {
                DepartmentInfo info = new DepartmentInfo();
                info.DepartmentName = obj.DelDepartmentName;
                var exist=DepartmentBLL.Instance.Get(0,info.DepartmentName);
                if (exist != null)
                {
                    result.Status = 0;
                    result.Message = "DepartmentName Is Exist";
                }
                else
                {
                    var depID = DepartmentBLL.Instance.Add(info);
                    result.FirstParam = depID;
                    result.Status = 1;
                    result.Message = "Requst Is Success";
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Department/Add Server Error";
                LogHelper.Error(ex.Message);
            }
            return result;
        }


        /// <summary>
        ///  获取部门列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> Get(DelPositionRequest obj)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {
                var list = DepartmentBLL.Instance.Get();
                result.FirstParam = list;
                result.Status = 1;
                result.Message = "Requst Is Success";
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Department/Remove Server Error";
                LogHelper.Error(ex.Message);
            }
            return result;
        }
    }
}